package br.com.DoceMagia.modelo.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import br.com.DoceMagia.admUtil.DaoUtil;
import br.com.DoceMagia.modelo.entidade.Usuario;

public class UsuarioDAO {

	private DaoUtil conexaoBanco;
	private String query = "";
	
	public UsuarioDAO() {
	conexaoBanco = new DaoUtil();	
	}

	public void insertUsuario(Usuario usuario) throws SQLException {

		Connection conn = null;
		PreparedStatement statement = null;

		try {
			int i = 0;
			conn = conexaoBanco.getConnection();
			query = "INSERT INTO USUARIOS (login,senha,nome,nivelAcesso) VALUES (?,?,?,?)";
			
			statement = conn.prepareStatement(query);
			
			statement.setString(++i, usuario.getLogin());
			statement.setString(++i, usuario.getSenha());
			statement.setString(++i, usuario.getNome());
			statement.setInt(++i, usuario.getNivelAcesso());

			statement.executeUpdate();
			
		} catch (Exception e1) {
			e1.printStackTrace();
		}finally{
			conexaoBanco.closeAll(conn, statement);
		}
	}
	public void updateUsuario(Usuario usuario) throws SQLException {
		
		Connection conn = null;
		PreparedStatement statement = null;
		
		try {
			int i = 0;
			conn = conexaoBanco.getConnection();
			query = "UPDATE USUARIO SET login = ?, senha=?, nome=?, nivelAcesso=? )";
			statement = conn.prepareStatement(query);
			
			statement.setString(++i, usuario.getLogin());
			statement.setString(++i, usuario.getSenha());
			statement.setString(++i, usuario.getNome());
			statement.setInt(++i, usuario.getNivelAcesso());
			
			statement.executeUpdate();

		} catch (Exception e1) {
			e1.printStackTrace();
		}finally{
			conexaoBanco.closeAll(conn, statement);
		}
		
	}
	
}
